//
//  APMeViewController.m
//  AqualPolo
//
//  Created by dong on 2024/5/1.
//

#import "APMeViewController.h"
#import "APMeHeaderView.h"
#import "APMeAvatarView.h"
#import "APMeItemControl.h"
#import "APLoginManager.h"
#import "SDWebImage.h"
#import "APPersonalEditViewController.h"
#import "TZImagePickerController.h"
#import "APOSSPolicyModel.h"
#import "APSettingViewController.h"
#import "APAboutViewController.h"
#import "APFollowListViewController.h"
#import "APBlockListViewController.h"
#import "APMyArticlesViewController.h"
#import "APCoinShopController.h"
#import "APHomeDataManager.h"
@interface APMeViewController ()<TZImagePickerControllerDelegate>
@property (nonatomic, strong) APMeHeaderView *headerView;
@property (nonatomic, strong) APMeAvatarView *avatarView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *idLabel;
@property (nonatomic, strong) UIButton *copybtn;
@property (nonatomic, strong)UIImageView *ballImageView;
@property (nonatomic, strong) APMeItemControl *myArticlesItem;
@property (nonatomic, strong) APMeItemControl *blockListItem;
@property (nonatomic, strong) APMeItemControl *aboutItem;
@property (nonatomic, strong) APMeItemControl *myFollowItem;

@end

@implementation APMeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadBackground:@"discovery_list_bg"];
    [self initUI];
    [self reloadData];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = true;
    WS(ws);
    [self.view ShowProgress];
    [[APLoginManager shareInstance] updateUserInfoByInternetSevice:^(_Bool success, NSString * _Nonnull msg) {
        [ws reloadData];
        [ws.view removeProgress];
    }];
    
}


- (void)reloadData{
    APUserModel *userModel = [[APLoginManager shareInstance] userInfo];
    _headerView.coinControl.titleLabel.text = [NSString stringWithFormat:@"%zd",userModel.availableCoins];
    [_avatarView.imageView sd_setImageWithURL:[NSURL URLWithString:userModel.avatarUrl]];
    _nameLabel.text = userModel.nickname;
    _idLabel.text = [NSString stringWithFormat:@"ID: %@",userModel.userId];
    self.ballImageView.image = [[APHomeDataManager shareInstance] currentUseBallImage];
}




- (void)coinButtonDidClick{
    APCoinShopController *coinShopVC = [[APCoinShopController alloc] init];
    [self.navigationController pushViewController:coinShopVC animated:true];
}

- (void)editButtonDidClick:(UIButton *)sender{
    APPersonalEditViewController *editVC = [[APPersonalEditViewController alloc] init];
    [self.navigationController pushViewController:editVC animated:true];
}

- (void)settingButtonDidClick:(UIButton *)sender{
    APSettingViewController *settingVC = [[APSettingViewController alloc]init];
    [self.navigationController pushViewController:settingVC animated:true];
}

- (void)myArticlesItemDidClick:(APMeItemControl *)sender{
    APMyArticlesViewController *myArticlesVC = [[APMyArticlesViewController alloc] init];
    [self.navigationController pushViewController:myArticlesVC animated:true];
}

- (void)blockListItemDidClick:(APMeItemControl *)sender{
    APBlockListViewController *blockListVc = [[APBlockListViewController alloc] init];
    [self.navigationController pushViewController:blockListVc animated:true];

}

- (void)myFollowItemDidClick:(APMeItemControl *)sender{
    APFollowListViewController *followListVC = [[APFollowListViewController alloc] init];
    [self.navigationController pushViewController:followListVC animated:true];
}

- (void)aboutItemDidClick:(APMeItemControl *)sender{
    APAboutViewController *aboutVC = [[APAboutViewController alloc] init];
    [self.navigationController pushViewController:aboutVC animated:true];
}

- (void)avatarViewDidClick{
    if (![APAuthorizationManager isCanUsePhotos]) {
        [self.view ShowProgressOnlyLabel:@"You have not authorised this permission."];
        return;
    }
    TZImagePickerController *imagePicker = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
    imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
    imagePicker.preferredLanguage = @"en";
    imagePicker.allowTakeVideo = false;
    imagePicker.allowPickingGif = false;
    imagePicker.allowPickingVideo = false;
    [self presentViewController:imagePicker animated:true completion:nil];
    [APLogManager statisticWithPage:APPage_report];
}

- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto{
    [self uploadImage:photos.firstObject];
}

- (void)uploadImage:(UIImage *)image{
    [self.view ShowProgress];
    [APDataManager getDataWithUrlString:ossPolicy result:^(NSDictionary * _Nullable res) {
        [self.view removeProgress];
        APOSSPolicyModel *model = [APOSSPolicyModel yy_modelWithDictionary:res[@"data"]];
        [self didUploadImage:image model:model];
    } failBlock:^(NSString * _Nullable errorMsg) {
        [self.view ShowProgressOnlyLabel:errorMsg];
    }];
}

- (void)didUploadImage:(UIImage *)image model:(APOSSPolicyModel *)ossModel{
    [self.view ShowProgress];
    [APDataManager uploadImagesWithUrl:ossModel.host policy:ossModel image:image progress:nil resultBlock:^(NSString * _Nonnull fileName) {
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        [parameters setNullObject:fileName forKey:@"avatarPath"];
        [APDataManager postJsonWithUrlString:updateAvatar timeoutInterval:0 parameters:parameters result:^(NSDictionary * _Nullable res) {
            APImageModel *imageModel =  [APImageModel yy_modelWithDictionary:res[@"data"]];
            [[APLoginManager shareInstance] updateAvatarInfo:imageModel];
            [self.avatarView.imageView sd_setImageWithURL:[NSURL URLWithString:imageModel.mediaUrl]];
            [self.view removeProgress];
        } failBlock:^(NSString * _Nullable errorMsg) {
            [self.view ShowProgressOnlyLabel:errorMsg];
        }];
    } failBlock:^(NSString * _Nonnull msg) {
        [self.view ShowProgressOnlyLabel:msg];
    }];
        
}





- (void)copyButtonDidClick:(UIButton *)sender{
    APUserModel *userModel = [[APLoginManager shareInstance] userInfo];
    [UIPasteboard generalPasteboard].string = userModel.userId;
    [self.view ShowProgressOnlyLabel:@"copy success"];
}


- (void)initUI{


    _headerView = [[APMeHeaderView alloc] init];
    [_headerView.coinControl addTarget:self action:@selector(coinButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
    [_headerView.editButton addTarget:self action:@selector(editButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [_headerView.settingButton addTarget:self action:@selector(settingButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_headerView];
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(25);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
    _avatarView = [[APMeAvatarView alloc] init];
    [_avatarView addTarget:self action:@selector(avatarViewDidClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_avatarView];
    [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_headerView.mas_bottom).offset(12);
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(140, 140));
    }];
    _nameLabel = [[UILabel alloc] init];
    _nameLabel.textColor = [UIColor whiteColor];
    _nameLabel.font = [UIFont systemFontOfSize:18];
    _nameLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:_nameLabel];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_avatarView.mas_bottom).offset(12);
        make.height.mas_equalTo(24);
        make.centerX.equalTo(self.view).offset(-16);
    }];
    UIImageView *iconImageView = [[UIImageView alloc] init];
    iconImageView.image = [UIImage imageNamed:@"ball_1"];
    self.ballImageView = iconImageView;
    [self.view addSubview:iconImageView];
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLabel.mas_right).offset(8);
        make.centerY.equalTo(_nameLabel);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    
    _idLabel = [[UILabel alloc] init];
    _idLabel.textColor = LYRGBAColor(255, 255, 255, 0.5);
    _idLabel.font = [UIFont systemFontOfSize:14];
    _idLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:_idLabel];
    [_idLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_nameLabel.mas_bottom).offset(13);
        make.height.mas_equalTo(14);
        make.centerX.equalTo(self.view).offset(-20);
    }];
    
    _copybtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_copybtn setTitle:@"Copy" forState:UIControlStateNormal];
    _copybtn.titleLabel.font = [LYFont fontWithSize:10 fontName:LYFontNamepfr];
    [_copybtn setTitleColor:LYRGBColor(87, 61, 255) forState:UIControlStateNormal];
    _copybtn.backgroundColor = LYRGBColor(158, 253, 223);
    _copybtn.layer.cornerRadius = 4;
    _copybtn.layer.masksToBounds = true;
    [_copybtn addTarget:self action:@selector(copyButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_copybtn];
    [_copybtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_idLabel.mas_right).offset(8);
        make.centerY.equalTo(_idLabel);
        make.size.mas_equalTo(CGSizeMake(32, 14));
    }];
    
    _myArticlesItem = [[APMeItemControl alloc] init];
    [_myArticlesItem addTarget:self action:@selector(myArticlesItemDidClick:) forControlEvents:UIControlEventTouchUpInside];
    _myArticlesItem.titleLabel.text = @"My Articles";
    _myArticlesItem.imageView.image = [UIImage imageNamed:@"myArticles_icon"];
    [self.view addSubview:_myArticlesItem];
    [_myArticlesItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_copybtn.mas_bottom).offset(32);
        make.right.equalTo(self.view.mas_centerX).offset(-16);
        make.size.mas_equalTo(CGSizeMake(126, 100));
    }];
    
    _myFollowItem = [[APMeItemControl alloc] init];
    [_myFollowItem addTarget:self action:@selector(myFollowItemDidClick:) forControlEvents:UIControlEventTouchUpInside];
    _myFollowItem.titleLabel.text = @"My follow";
    _myFollowItem.imageView.image = [UIImage imageNamed:@"myFollow_icon"];
    [self.view addSubview:_myFollowItem];
    [_myFollowItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_copybtn.mas_bottom).offset(32);
        make.left.equalTo(self.view.mas_centerX).offset(16);
        make.size.mas_equalTo(CGSizeMake(126, 100));
    }];
    
    _blockListItem = [[APMeItemControl alloc] init];
    [_blockListItem addTarget:self action:@selector(blockListItemDidClick:) forControlEvents:UIControlEventTouchUpInside];
    _blockListItem.titleLabel.text = @"Block List";
    _blockListItem.imageView.image = [UIImage imageNamed:@"blockList_icon"];
    [self.view addSubview:_blockListItem];
    [_blockListItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_myArticlesItem.mas_bottom).offset(12);
        make.left.equalTo(_myArticlesItem);
        make.size.mas_equalTo(CGSizeMake(126, 100));
    }];
    
    _aboutItem = [[APMeItemControl alloc] init];
    [_aboutItem addTarget:self action:@selector(aboutItemDidClick:) forControlEvents:UIControlEventTouchUpInside];
    _aboutItem.titleLabel.text = @"About";
    _aboutItem.imageView.image = [UIImage imageNamed:@"about_icon"];
    [self.view addSubview:_aboutItem];
    [_aboutItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_blockListItem);
        make.left.equalTo(_myFollowItem);
        make.size.mas_equalTo(CGSizeMake(126, 100));
    }];
    
    
}


- (void)viewSafeAreaInsetsDidChange{
    [super viewSafeAreaInsetsDidChange];
    [_headerView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(5 + self.view.safeAreaInsets.top);

    }];
    
    
}

@end
